package cc.sunni.api;

import cc.sunni.common.Constant;
import cc.sunni.common.utils.JsonUtils;
import cc.sunni.common.utils.R;
import cc.sunni.common.utils.RedisOperator;
import cc.sunni.pojo.vo.AppUserVO;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class BaseController {
    @Autowired
    public RedisOperator redis;

    /**
     * 获取BO中的错误信息
     *
     * @param result
     */
    public Map<String, String> getErrors(BindingResult result) {
        Map<String, String> map = new HashMap<>();
        List<FieldError> errorList = result.getFieldErrors();
        for (FieldError error : errorList) {
            // 发送验证错误的时候所对应的某个属性
            String field = error.getField();
            // 验证的错误消息
            String msg = error.getDefaultMessage();
            map.put(field, msg);
        }
        return map;
    }

    public void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, Integer maxAge) {
        try {
            cookieValue = URLEncoder.encode(cookieValue, "utf-8");
            setCookieValue(request, response, cookieName, cookieValue, maxAge);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void setCookieValue(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, Integer maxAge) {
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setMaxAge(maxAge);
        cookie.setDomain("imoocnews.com");
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        try {
            String deleteValue = URLEncoder.encode("", "utf-8");
            setCookieValue(request, response, cookieName, deleteValue, Constant.COOKIE_DELETE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    public Integer getCountsFromRedis(String key) {
        String countsStr = redis.get(key);
        if (StringUtils.isBlank(countsStr)) {
            countsStr = "0";
        }
        return Integer.valueOf(countsStr);
    }

    public List<AppUserVO> getBasicUserList(Set idSet) {
        String userServerUrlExecute = "http://user.imoocnews.com:8003/user/queryByIds?userIds=" + JSONUtil.toJsonStr(idSet);
        String responseEntity = HttpUtil.get(userServerUrlExecute);
        R r = JSONUtil.toBean(responseEntity, R.class);
        List<AppUserVO> userVOList = new ArrayList<>();
        if (200==(Integer) r.get("status")) {
            String userJson = JSONUtil.toJsonStr(r.get("data"));
            userVOList = JSONUtil.toList(userJson, AppUserVO.class);
        }
        return userVOList;
    }

}
